package com.dxl.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
@Api(tags = "jdbc模块")
@RequestMapping("/jdbc")
@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/addUser")
    public String addUser(){
        String sql="insert into mydb.admin(id,name,password) values (3,'dxl','dxlani001')";
        jdbcTemplate.update(sql);
        return "addUser ok";
    }
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        String sql="delete from mydb.admin where id=?";
        jdbcTemplate.update(sql,id);
        return "deleteUser ok";
    }
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql="update mydb.admin set name=?,password=? where id="+id;
        Object[] objects =new Object[2];
        objects[0]="test";
        objects[1]="123456";
        jdbcTemplate.update(sql,objects);
        return "updateUser ok";
    }
    @GetMapping("/userList")
    public List<Map<String ,Object>> userList(){
        String sql="select * from admin";
        List<Map<String ,Object>> list_maps=jdbcTemplate.queryForList(sql);
        return list_maps;
    }

//    @RequestMapping(value = "/loginpage")
//    public String index(Model model) {
//        String name = "jiangbei";
//        model.addAttribute("name", name);
//        return "hello";
//    }
}
